package cn.javacv.gulimall.product.service.impl;

import cn.javacv.gulimall.product.dao.AttrGroupDao;
import cn.javacv.gulimall.product.entity.AttrAttrgroupRelationEntity;
import cn.javacv.gulimall.product.entity.AttrEntity;
import cn.javacv.gulimall.product.service.AttrService;
import cn.javacv.gulimall.product.vo.AttrGroupWithAttrsVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.javacv.gulimall.common.utils.PageUtils;
import cn.javacv.gulimall.common.utils.Query;

import cn.javacv.gulimall.product.entity.AttrGroupEntity;
import cn.javacv.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrService attrService ;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params,long categoryId) {
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        Object key = params.get("key");
        if (null != key){
            wrapper.and((obj) -> {
                obj.eq("id",key).or().like("name",key);
            });
        }

        if (0 == categoryId) {
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }else{
            wrapper.eq("category_id",categoryId);
            IPage<AttrGroupEntity> page = this.page(
                    new Query<AttrGroupEntity>().getPage(params),
                    wrapper
            );
            return new PageUtils(page);
        }
    }

    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrByCategoryId(long catelogId) {
        List<AttrGroupEntity> AttrGroupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("category_id",catelogId));
        List<AttrGroupWithAttrsVO> list = AttrGroupEntityList.stream().map(
                attrGroupEntity -> {
                    AttrGroupWithAttrsVO attrGroupWithAttrsVO = new AttrGroupWithAttrsVO();
                    BeanUtils.copyProperties(attrGroupEntity,attrGroupWithAttrsVO);
                    List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrGroupEntity.getId());
                    attrGroupWithAttrsVO.setAttrs(attrEntityList);
                    return attrGroupWithAttrsVO;
                }
        ).collect(Collectors.toList());

        return list;
    }

}
